pub mod crc32_ {
    use crc::{Crc, CRC_32_ISO_HDLC};
    use std::{io::Read, io::Write};
    pub fn encode(r: &mut dyn Read, w: &mut dyn Write) {
        let crc = Crc::<u32>::new(&CRC_32_ISO_HDLC);
        let mut digest = crc.digest();
        let mut v = [0u8; 1024];
        loop {
            let cnt = r.read(&mut v[..]).unwrap();
            if cnt == 0 {
                break;
            }
            digest.update(&mut v[..cnt])
        }
        let checksum = format!("{0:<8X}\n", digest.finalize());
        // println!("result is {0:8}", checksum);
        let re = w.write(checksum.as_bytes());
        if re.is_err() {
            println!("err is {:#} ", re.expect_err("write error"))
        }
    }
}

pub mod md5 {
    use chksum_md5 as md5;
    use std::{io::Read, io::Write};
    pub fn encode(r: &mut dyn Read, w: &mut dyn Write) {
        let mut f = md5::reader::new(r);
        let mut buffer = Vec::new();
        f.read_to_end(&mut buffer).unwrap();
        let digest = f.digest();
        let checksum = format!("{}\n", digest.to_string().to_uppercase());
        // println!("result is {}", checksum);
        let re = w.write(checksum.as_bytes());
        if re.is_err() {
            println!("err is {:#} ", re.expect_err("write error"))
        }
    }
}

pub mod sha1 {
    use sha1_smol::Sha1 as sha1;
    use std::{io::Read, io::Write};
    pub fn encode(r: &mut dyn Read, w: &mut dyn Write) {
        let mut digest = sha1::new();
        let mut v = [0u8; 1024];
        loop {
            let cnt = r.read(&mut v[..]).unwrap();
            if cnt == 0 {
                break;
            }
            digest.update(&mut v[..cnt])
        }
        let checksum = format!("{}\n", digest.digest().to_string().to_ascii_uppercase());
        // println!("result is {}", checksum);
        let re = w.write(checksum.as_bytes());
        if re.is_err() {
            println!("err is {:#} ", re.expect_err("write error"))
        }
    }
}
